package lero.service.impl;

import lero.common.MD5Util;
import lero.mapper.CategoryMapper;
import lero.pojo.Category;
import lero.service.CategoryServiceI;
import lero.vo.CategoryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Categoryistrator on 2018/7/4.
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryServiceI {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void insert(Category category) {
        //新增顶级类别处理
        if(StringUtils.isEmpty(category.getParent().getId())){
            category.getParent().setId(0);
        }
        categoryMapper.insert(category);
    }

    @Override
    public List<Category> select(CategoryVo vo) {

        return categoryMapper.select(vo);
    }

    @Override
    public Category selectOne(int id) {
        return categoryMapper.selectOne(id);
    }

    @Override
    public void delete(int id) {
        categoryMapper.delete(id);
    }

    @Override
    public void deleteMany(int[] ids) {
        categoryMapper.deleteMany(ids);
    }


    @Override
    public void update(Category category) {
        if(StringUtils.isEmpty(category.getParent().getId())){
            category.getParent().setId(0);
        }
        categoryMapper.update(category);
    }

    @Override
    public List<Map<String,Object>> getTree() {
        List<Map<String,Object>> sourceData = categoryMapper.getTree();
        List<Map<String,Object>> targetTree = new ArrayList<>();
        //顶级
        for (Map<String,Object> c:sourceData){
            System.out.println(c);
        }
        for (Map<String,Object> map:sourceData){
            // 顶级没有pid
            if (StringUtils.isEmpty(map.get("pid"))) {
                targetTree.add(map);
            }
        }
        //递归设置顶级
        for (Map<String,Object> map:targetTree){
            Integer id = (Integer) map.get("id");
            map.put("children",buildChild(id,sourceData));
        }
        /*   nodes: [{ //节点
            name: '父节点1'
                    ,children: [{
                name: '子节点11'
            },{
                name: '子节点12'
            }]
        },{
            name: '父节点2（可以点左侧箭头，也可以双击标题）'
                    ,children: [{
                name: '子节点21'
                        ,children: [{
                    name: '子节点211'
                }]
            }]
        }]*/

        return  targetTree;
    }

    private  List<Map<String,Object>> buildChild(int pid,List<Map<String,Object>> sourceData){
        //子分类
        List<Map<String,Object>>  children = new ArrayList<>();

        for (Map<String,Object> map:sourceData){
            if(!StringUtils.isEmpty(map.get("pid"))){
                if(map.get("pid").equals(pid)){
                    children.add(map);
                }
            }
        }
        // 把子分类的分类再循环一遍
        for (Map<String,Object> map : children) {
            if (!StringUtils.isEmpty(map.get("pid"))) {
                // 递归
                Integer id = (Integer) map.get("id");
                map.put("children",buildChild(id,sourceData));
            }
        } // 递归退出条件
        if (children.size() == 0) {
            return null;
        }
        return children;
    }
}
